package servlet;

import dao.ResumeMapper;
import entity.Resume;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/AddServlet")
public class AddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("test/html;charset=utf-8");

        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        Resume resume = new Resume();
        resume.setUserName(req.getParameter("userName"));
        resume.setSex(req.getParameter("sex"));
        resume.setAddress(req.getParameter("address"));
        resume.setBirth(req.getParameter("birth"));
        resume.setTelephone(req.getParameter("telephone"));
        resume.setSchool(req.getParameter("school"));
        resume.setStatus(Integer.parseInt(req.getParameter("status")));
        req.getSession().setAttribute("msg",sqlSession.getMapper(ResumeMapper.class).resumeAdd(resume) > 0);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        resp.sendRedirect("ListServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
